package com.dxhackday.ppicker.model;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * User: arusakov
 * Date: 24.11.12
 * Time: 13:10
 */
public class PhotoManager {

    public static final int PREFETCH_SIZE = 20;

    public static List<Photo> getPictureList(String baseDir) {
        File[] files = new File(baseDir).listFiles();

        if (files == null)
            return Collections.emptyList();

        ArrayList<Photo> photos = new ArrayList<>();
        for (File f : files) {
            if (f.isDirectory())
                continue;

            photos.add(new Photo(f));
        }

        return photos;
    }

    public static void savePictureList(String base, List<Photo> photos){
        File basefile = new File(base);
        if (!basefile.exists()){
            basefile.mkdirs();
        }
        ListIterator<Photo> iterator = (ListIterator<Photo>) photos.iterator();
        while (iterator.hasNext()){
            Photo photo = iterator.next();
            try {
                //        System.out.println(photo);
                IOUtils.copy(new FileInputStream(photo.getFile()),
                        new FileOutputStream(base + "/" + photo.getFile().getName()))   ;
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    private HashMap<String, List<Photo>> lists = new HashMap<>();
    private List<Photo> currentList;
    private int currentIndex;
    private boolean currentMoved;

    public List<Photo> getList(String id) {
        return lists.get(id);
    }

    public List<Photo> getCurrentList() {
        return currentList;
    }

    public void addPictureList(String id, List<Photo> list) {
        lists.put(id, list);
        if (currentList == null)
            setCurrentList(id);
    }

    public void setCurrentList(String id) {
        if (!lists.containsKey(id)) {
            throw new NoSuchElementException(id);
        }
        currentList = lists.get(id);
        currentIndex = -1;
        currentMoved = false;
        prefetchFxImages();
    }

    public Photo next() {
        if (currentIndex >= 0)
            currentList.get(currentIndex).disposeFxImage();
        currentIndex++;
        currentMoved = false;
        Photo photo = currentList.get(currentIndex);
        prefetchFxImages();
        return photo;
    }

    public boolean hasNext() {
        return (currentIndex + 1) < currentList.size();
    }

    public void moveCurrentPhotoToList(String newList) {
        if (currentIndex >= 0 && !currentMoved) {
            Photo photo = currentList.remove(currentIndex);
            currentIndex--;
            currentMoved = true;
            lists.get(newList).add(photo);
        }
    }

    private void prefetchFxImages() {
        if (currentList.isEmpty())
            return;

        for (int index = Math.max(0, currentIndex); index < currentIndex + PREFETCH_SIZE; index++) {
            currentList.get(index % currentList.size()).getFxImage();
        }
    }

    public void copyToList(String newList) {
        lists.get(newList).add(currentList.get(currentIndex));
    }


    public int getCurrentIndex() {
        return currentIndex;
    }

    public Photo getCurrentPhoto() {
        if (currentIndex < 0)
            return null;
        return currentList.get(currentIndex);
    }
}
